//
//  ZJSIScopeView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/13.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJSIScopeView.h"

@interface ZJSIScopeView ()
{
    UIButton *_selectButton;
}
@end

@implementation ZJSIScopeView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame title:title];
    if (self) {
        
    }
    return self;
}

- (void)updataUI_dataArray:(NSArray *)dataArray selectString:(NSString *)selectString{
    for (id view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [(UIButton *)view removeFromSuperview];
        }
    }
    CGFloat maxHeight = 0;
    
    __block NSMutableArray *buttonWs= [NSMutableArray array];
    __block CGFloat maxWidth = 0.0;
    [dataArray enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat width = [NSString zj_sizeOfString:obj font:[UIFont titleTextFont] width:[UIScreen zj_screenWith]].width + 45 * [UIScreen zj_screenWScale];
        maxWidth += width;
        [buttonWs addObject: @(width)];
    }];
    
    CGFloat top = self.titleLabel.bottom;
    CGFloat left = 12 * [UIScreen zj_screenWScale];
    CGFloat space = (self.width - 2 * left - maxWidth) / (dataArray.count - 1);
    CGFloat imageH = 35 * [UIScreen zj_screenHScale];
    
    CGFloat maxLeft = left;
    for (int i = 0; i < dataArray.count; i++) {
        CGFloat imageW = [buttonWs[i] floatValue];
        NSInteger zheng = i / dataArray.count;
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(maxLeft, (imageH + space) * zheng + top, imageW, imageH)];
        
        button.layer.borderWidth = 1;
        button.layer.borderColor = [UIColor zj_colorWithHexString:@"ebebeb"].CGColor;
        button.layer.cornerRadius = 4 * [UIScreen zj_screenWScale];
        button.tag = i;
        button.titleLabel.font = [UIFont titleTextFont];
        [button setTitle:dataArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        maxLeft += imageW + space;
        maxHeight = button.bottom;
        if (!selectString && i == 0) {
            [self setButton:button isSelect:YES];
            _selectButton = button;
        }else if (selectString && i == [selectString integerValue]) {
            [self setButton:button isSelect:YES];
            _selectButton = button;
        }else{
            [self setButton:button isSelect:NO];
        }
        [self addSubview:button];
        [button addTarget:self action:@selector(action_buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.height = maxHeight + 6 * [UIScreen zj_screenHScale];
}

- (void)minUpdataUI_dataArray:(NSArray *)dataArray selectString:(NSString *)selectString{
    for (id view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [(UIButton *)view removeFromSuperview];
        }
    }
    CGFloat maxHeight = 0;
    
    CGFloat top = self.titleLabel.bottom;
    CGFloat left = 12 * [UIScreen zj_screenWScale];
    CGFloat space = 25 * [UIScreen zj_screenWScale];
    CGFloat imageW = (self.width - 2 * left - (dataArray.count - 1) * space) / dataArray.count;
    CGFloat imageH = 35 * [UIScreen zj_screenHScale];
    for (int i = 0; i < dataArray.count; i++) {
        NSInteger yu = i % dataArray.count;
        NSInteger zheng = i / dataArray.count;
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(left + yu * (imageW + space), (imageH + space) * zheng + top, imageW, imageH)];
    
        button.layer.borderWidth = 1;
        button.layer.borderColor = [UIColor zj_colorWithHexString:@"ebebeb"].CGColor;
        button.layer.cornerRadius = 4 * [UIScreen zj_screenWScale];
        button.tag = i;
        button.titleLabel.font = [UIFont titleTextFont];
        [button setTitle:dataArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
     
        maxHeight = button.bottom;
        if (!selectString && i == 0) {
            [self setButton:button isSelect:YES];
            _selectButton = button;
        }else if (selectString && i == [selectString integerValue]) {
            [self setButton:button isSelect:YES];
            _selectButton = button;
        }else{
            [self setButton:button isSelect:NO];
        }
        [self addSubview:button];
        [button addTarget:self action:@selector(action_buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.height = maxHeight + 6 * [UIScreen zj_screenHScale];
}


- (void)setButton:(UIButton *)button isSelect:(BOOL)isSelect{
    button.selected = isSelect;
    if (isSelect) {//选中
        button.backgroundColor = [UIColor themeColor];
        button.layer.borderWidth = 0;
    }else{//未选中
        button.backgroundColor = [UIColor clearColor];
        button.layer.borderWidth = 1;
    }
}


#pragma mark - 点击事件
- (void)action_buttonClick:(UIButton *)sender{
//    if (sender.selected) {
//        return;
//    }
    [self setButton:_selectButton isSelect:NO];
    [self setButton:sender isSelect:YES];
    _selectButton = sender;
    if (self.delegate) {
        [self.delegate ZJSIScopeViewClick_scopeView:self index:sender.tag];
    }
}

@end
